package thinkingInJava.io;
// page 553
//: io/GetChannel.java
//Getting channels from streams
import java.nio.*;
import java.nio.channels.*;
import java.io.*;

public class GetChannel {
	private static final int BSIZE = 1024;
	public static void main(String[] args) throws Exception {
		// Write a file:
		FileChannel fc =
				new FileOutputStream("data.txt").getChannel();
		fc.write(ByteBuffer.wrap("Some text ".getBytes()));
		fc.close();
		// Add to the end of the file:
		fc =
				new RandomAccessFile("data.txt", "rw").getChannel();
		fc.position(fc.size()); // Move to the end
		fc.write(ByteBuffer.wrap("Some more".getBytes()));
		fc.close();
		// Read the file:
		fc = new FileInputStream("data.txt").getChannel();
		ByteBuffer buff = ByteBuffer.allocate(BSIZE);
		fc.read(buff);
		
		// 一旦调用 read() 来告知 FileChannel 向 ByteBuffer 存储字节，就必须调用
		// 缓冲器上的flip(), 让它做好让别人读取字节的立体图（这似乎有一点拙劣，
		// 但却适用于获取最大速度）
		buff.flip();
		while(buff.hasRemaining())
			System.out.print((char)buff.get());
	}
} /* Output:
Some text Some more
 *///:~
